<template>
  <div class="banxin2">
    <div class="hot">
      <div class="title">
        <h3>热门新碟</h3>
      </div>
      <div class="content">
        <div class="c-box"
             v-for="item in albums_new"
             :key="item.id">
          <div class="image">
            <img :src="item.picUrl"
                 alt="">
          </div>
          <h2 class="eli">{{ item.name }}</h2>
          <p class="eli">{{ item.author }}</p>
        </div>
      </div>
    </div>
    <div class="all">
      <div class="title">
        <h3>全部新碟</h3>
        <ul>
          <li v-for="item in all_bar"
              :key="item.area"
              @click="areaChange(item.area)">{{ item.name }}</li>
        </ul>
      </div>
      <div class="content">
        <div class="c-box"
             v-for="item in albums"
             :key="item.id">
          <div class="image">
            <img :src="item.picUrl"
                 alt="">
          </div>
          <h2 class="eli">{{ item.name }}</h2>
          <p class="eli">{{ item.author }}</p>
        </div>
      </div>
      <div class="page">
        <el-pagination background
                       layout="prev,pager,next"
                       :total="totalPage"
                       :current-page="currentPage"
                       :page-size="pageSize"
                       @current-change="currentChange"></el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import { albumInfo, albumInfo_new } from '@/assets/js/request'
export default {
  data() {
    return {
      all_bar: [
        { name: '全部', area: 'ALL' },
        { name: '华语', area: 'ZH' },
        { name: '欧美', area: 'EA' },
        { name: '韩国', area: 'KR' },
        { name: '日本', area: 'JP' },
      ],
      albums: [{ id: null, name: '', picUrl: '', author: '' }],
      albums_new: [{ id: null, name: '', picUrl: '', author: '' }],
      currentPage: 1,
      totalPage: 1000,
      pageSize: 30,
      area: 'ALL',
    }
  },
  created() {
    this.doAlbumInfo_new()
    this.doAlbumInfo(this.area)
  },
  methods: {
    currentChange(index) {
      if (index != this.currentPage) {
        this.currentPage = index
      }
      this.doAlbumInfo(this.area)
    },
    areaChange(area) {
      this.area = area
      this.currentPage = 1
      this.doAlbumInfo(area)
    },
    doAlbumInfo(area) {
      let offset = (this.currentPage - 1) * this.pageSize
      albumInfo({ area, limit: this.pageSize, offset }).then((res) => {
        this.totalPage = res.total
        let list = []
        res.albums.forEach((item) => {
          let { id, name, picUrl, author } = {
            ...item,
            author: item.artist.name,
          }
          list.push({ id, name, picUrl, author })
        })
        this.albums = list
      })
    },
    doAlbumInfo_new() {
      albumInfo_new().then((res) => {
        let list = []
        res.albums.forEach((item, index) => {
          if (index > 9) return
          let { id, name, picUrl, author } = {
            ...item,
            author: item.artist.name,
          }
          list.push({ id, name, picUrl, author })
        })
        this.albums_new = list
      })
    },
  },
}
</script>

<style lang="less" scoped>
@import '@/assets/css/base.less';
.eli {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.banxin2 {
  padding: 40px;
  border: 1px solid #ccc;
  box-sizing: border-box;
  text-align: left;
  .page {
    text-align: center;
  }
}
.title {
  font-size: 12px;
  color: #333;
  width: 100%;
  border-bottom: 2px solid #c20c0c;
  margin-bottom: 20px;
  &::after {
    content: '.';
    display: block;
    clear: both;
    visibility: hidden;
    height: 0;
  }
  h3 {
    font-size: 24px;
    float: left;
    line-height: 40px;
  }
  ul {
    float: left;
    margin: 12px 0 0 5px;
    display: flex;
    justify-content: flex-start;
    li {
      padding: 0 15px;
      border-right: 1px solid #ccc;
      cursor: pointer;
      color: #666;
      &:last-of-type {
        border: none;
      }
      &:hover {
        text-decoration: underline;
      }
    }
  }
}
.content {
  display: flex;
  justify-content: flex-start;
  flex-wrap: wrap;
  gap: 20px;
  margin-bottom: 40px;
  .c-box {
    width: 153px;
    line-height: 24px;
    .image {
      width: 100%;
      height: 130px;
      background: url(@/assets/image/coverall.png) no-repeat 0 9999px;
      background-position: 0 -845px;
      img {
        width: 130px;
        height: 130px;
      }
    }
    h2 {
      width: 100%;
      font-size: 14px;
      color: #000;
    }
    p {
      width: 100%;
      font-size: 12px;
      color: #999;
    }
  }
}
</style>